package ru.spb.osll.myfit.server.execute;

import org.easymock.EasyMock;
import org.junit.Test;
import ru.spb.osll.myfit.common.ExecutingResult;
import ru.spb.osll.myfit.common.PageExecutingResult;
import ru.spb.osll.myfit.common.PageProgressCounts;
import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.util.FileHelper;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.TreeMap;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class PageExecutorTest {
	@Test
	public void testPageExecutorRun() throws Exception {
		Date now = new Date();
		String uname = "TestPage" + FileHelper.SEPARATOR + FileHelper.DATE_FORMATTER.format(now);
		FitNesseTestRunner runner = EasyMock
				.createMock(FitNesseTestRunner.class);
		FixtureListenerImpl fixtureListener = EasyMock
				.createMock(FixtureListenerImpl.class);
		PageWriter pageWriter = EasyMock.createMock(PageWriter.class);
		FileHelper fileHelper = EasyMock.createMock(FileHelper.class);
		runner.run("TestPage");
		EasyMock.expectLastCall();

		ArrayList<Table> tables = new ArrayList<Table>();
		tables.add(new Table());
		PageProgressCounts counts = new PageProgressCounts();
		EasyMock.expect(fixtureListener.getTables()).andReturn(tables);
		EasyMock.expect(fixtureListener.getCounts()).andReturn(counts);
		EasyMock.expect(fixtureListener.getOutputStream()).andReturn(
		    new ByteArrayOutputStream());
		OutputStream metadataStream = new ByteArrayOutputStream();
		EasyMock.expect(fileHelper.openFile("TestPage.metadata")).andReturn(metadataStream);
		fileHelper.close(metadataStream);
		EasyMock.expectLastCall();
		
		EasyMock.replay(runner, fixtureListener, fileHelper);
		

		PageExecutor executor = new PageExecutor(runner, "TestPage",
				fixtureListener, pageWriter, fileHelper, now, uname, "deps", "fixtures", false);
		executor.setMetadata(new TreeMap<String, String>(), "TestPage.metadata");

		executor.run();
		PageExecutingResult result = executor.getPageExecutingResult();
		assertEquals(ExecutingResult.ExecutingState.FINISH, result.getState());
		assertFalse(result.getExecutedTables().isEmpty());
		EasyMock.verify(runner, fixtureListener, fileHelper);
	}
	
	 @Test
	  public void testPageExecutorDeleteDeps() throws Exception {
	    Date now = new Date();
	    String uname = "TestPage" + FileHelper.SEPARATOR + FileHelper.DATE_FORMATTER.format(now);
	    FitNesseTestRunner runner = EasyMock
	        .createMock(FitNesseTestRunner.class);
	    FixtureListenerImpl fixtureListener = EasyMock
	        .createMock(FixtureListenerImpl.class);
	    PageWriter pageWriter = EasyMock.createMock(PageWriter.class);
	    FileHelper fileHelper = EasyMock.createMock(FileHelper.class);
	    runner.run("TestPage");
	    EasyMock.expectLastCall();
	    OutputStream metadataStream = new ByteArrayOutputStream();
	    EasyMock.expect(fileHelper.openFile("TestPage.metadata")).andReturn(metadataStream);
	    fileHelper.close(metadataStream);
	    EasyMock.expectLastCall();

	    ArrayList<Table> tables = new ArrayList<Table>();
	    tables.add(new Table());
	    PageProgressCounts counts = new PageProgressCounts();
	    EasyMock.expect(fixtureListener.getTables()).andReturn(tables);
	    EasyMock.expect(fixtureListener.getCounts()).andReturn(counts);
	    EasyMock.expect(fixtureListener.getOutputStream()).andReturn(
	        new ByteArrayOutputStream());
	    fileHelper.delete("deps");
	    EasyMock.expectLastCall();
	    fileHelper.delete("fixtures");
	    EasyMock.expectLastCall();
	    EasyMock.replay(runner, fixtureListener, fileHelper);
	    

	    PageExecutor executor = new PageExecutor(runner, "TestPage",
	        fixtureListener, pageWriter, fileHelper, now, uname, "deps", "fixtures", true);
	    executor.setMetadata(new TreeMap<String, String>(), "TestPage.metadata");

	    executor.run();
	    PageExecutingResult result = executor.getPageExecutingResult();
	    assertEquals(ExecutingResult.ExecutingState.FINISH, result.getState());
	    assertFalse(result.getExecutedTables().isEmpty());
	    EasyMock.verify(runner, fixtureListener, fileHelper);
	  }
}
